package com.shiku.imclient;

import com.shiku.imserver.common.message.AuthMessage;
import com.shiku.imserver.common.message.ChatMessage;
import com.shiku.imserver.common.message.MessageHead;
import com.shiku.imserver.common.packets.ImPacket;
import com.shiku.imserver.common.proto.MessageProBuf;
import com.shiku.imserver.common.utils.Callback;
import com.shiku.imserver.common.utils.ProBufUtils;
import com.shiku.imserver.common.utils.StringUtils;
import com.shiku.imserver.common.utils.ThreadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;

public class IMClientStarter {
    static BaseClientHandler clientHandler = new BaseClientHandler() {
        /* class com.shiku.imclient.IMClientStarter.AnonymousClass1 */

        public void handlerReceipt(String messageId) {
        }

        public Packet heartbeatPacket(ChannelContext channelContext) {
            MessageHead messageHead = new MessageHead();
            ChatMessage message = new ChatMessage();
            messageHead.setChatType((byte) 1);
            messageHead.setFrom(channelContext.userid + "/Server");
            messageHead.setTo("200000");
            message.setFromUserId(channelContext.userid);
            message.setFromUserName(channelContext.userid);
            message.setToUserId("200000");
            message.setToUserName("200000");
            message.setContent("1111111");
            message.setType((short)1);
            message.setMessageHead(messageHead);
            ImPacket packet = ProBufUtils.encodeTcpPacket(message, MessageProBuf.ChatMessage.getDescriptor());
            packet.setCommand((short)10);
            return packet;
        }
    };
    static BaseClientListener clientListener = new BaseClientListener() {
        /* class com.shiku.imclient.IMClientStarter.AnonymousClass2 */

        public AuthMessage authUserMessage(ChannelContext channelContext, BaseIMClient imClient) {
            return null;
        }
    };
    static final Logger log = LoggerFactory.getLogger(BaseIMClient.class);
    static long seelp = 10000;
    static boolean sendMsg = true;
    static String serverIp = "150.109.127.138";
    static int serverPort = 5666;

    public static void main(String[] args) throws Exception {
        int connCount = 100;
        if (1 < args.length) {
            try {
                connCount = Integer.valueOf(args[1]).intValue();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        if (2 < args.length) {
            try {
                serverIp = args[2];
            } catch (Exception e2) {
                log.error(e2.getMessage());
            }
        }
        if (3 < args.length) {
            try {
                sendMsg = "1".equals(args[3]);
            } catch (Exception e3) {
                log.error(e3.getMessage());
            }
        }
        if (4 < args.length) {
            try {
                seelp = Long.valueOf(args[4]).longValue();
            } catch (Exception e4) {
                log.error(e4.getMessage());
            }
        }
        System.out.println("connCount  =====> " + connCount);
        System.out.println("serverIp  =====> " + serverIp);
        System.out.println("sendMsg  =====> " + sendMsg);
        System.out.println("seelp  =====> " + seelp);
        
        for (int i = 0; i < connCount; i++) {
            ThreadUtil.executeInThread(new Callback() {
                /* class com.shiku.imclient.IMClientStarter.AnonymousClass3 */

                public void execute(Object obj) {
                	IMClient client = new IMClient();
                    String resource = StringUtils.randomCode();
                    client.setUserId(System.currentTimeMillis() + "");
                    client.setResource(resource);
                    try {
                        client.initIMClient(IMClientStarter.serverIp, IMClientStarter.serverPort, IMClientStarter.clientHandler, IMClientStarter.clientListener);
                    } catch (Exception e) {
                        IMClientStarter.log.error(e.toString(), e);
                    }
                }
            });
        }
    }

    public static void whileSendMsg(IMClient imClient) {
        try {
            Thread.sleep(seelp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true) {
            MessageHead messageHead = new MessageHead();
            ChatMessage message = new ChatMessage();
            messageHead.setChatType((byte) 1);
            messageHead.setFrom(imClient.getConnStr());
            messageHead.setTo("200000");
            message.setFromUserId(imClient.getUserId());
            message.setFromUserName(imClient.getUserId());
            message.setToUserId("200000");
            message.setToUserName("200000");
            message.setContent("1111111");
            message.setType((short)1);
            message.setMessageHead(messageHead);
            imClient.sendMessage(message);
            try {
                Thread.sleep(seelp);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
    }
}
